/**
 * @description:实现线程交流使用两个线程打印100内的数，线程1，线程2交替打印
 * @author:Mr.Cat
 * @create:2021-09-22 20:44
 **/
public class ThreadCommunicationTest {
    public static void main(String[] args) {
        number n1 = new number();
        Thread t1 = new Thread(n1);
        Thread t2 = new Thread(n1);

        t1.setName("线程1：");
        t2.setName("线程2：");

        t1.start();
        t2.start();
    }
}

class number implements Runnable {
    private int numberp = 1;

    @Override
    public synchronized void run() {

        while (true) {
            synchronized (this) {
                notify();
                if (numberp <= 100) {

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + numberp);
                    numberp++;
                    try {
                        //使用调用如下wait()方法的线程进入阻塞状态
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    break;
                }
            }
        }
    }
}